import Toast from 'tdesign-miniprogram/toast/index';
import requestApi from "../../utils/request"
import util from "../../utils/util"

Page({
  data: {
    imgSrcs: [],
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: { type: 'dots' },
    swiperImageProps: { mode: 'scaleToFill' },
    searchValue: '',
    defaultIndex: ''
  },

  goodListPagination: {
    index: 1,
    num: 20,
  },

  privateData: {
    tabIndex: 0,
  },

  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3
      })
    }
  },

  onLoad() {
    this.init();
  },

  onReachBottom() {
    this.goodListPagination.index = this.goodListPagination.index + 1
    this.loadGoodsList()
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadHomePage();
  },

  async loadHomePage() {
    wx.stopPullDownRefresh();

    this.setData({
      pageLoading: true,
    });
    const img = await requestApi.get('shopro/index/baneer', '', true)
    requestApi.post('shopro/category/index?id=43', '', true).then(res => {
      let tabList = res.data.children
      this.setData({
        tabList,
        imgSrcs: img.data.index,
        pageLoading: false,
        defaultIndex: tabList[0].id
      });
      this.privateData.tabIndex = {
        value: tabList[0].id
      }
      this.loadGoodsList(true, tabList[0].id);
    })
  },

  tabChangeHandle(e) {
    this.privateData.tabIndex = e.detail;
    this.loadGoodsList(true);
  },

  onReTry() {
  },
  //下拉刷新
  async loadGoodsList(fresh = false, id) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }
    this.setData({ goodsListLoadStatus: 1 });
    const pageSize = this.goodListPagination.num;
    let pageIndex = this.goodListPagination.index;
    if (fresh) {
      pageIndex = 1;
    }
    try {
      let params = {
        'category_id': id ? id : this.privateData.tabIndex.value,
        'page': pageIndex,
        'per_page': pageSize
      }
      const nextList = await requestApi.get('shopro/goods/lists', params)
      if (nextList.data.data.length > 0) {
        nextList.data.data.forEach(item => {
          item.price = item.price * 100
        })
      }
      this.setData({
        goodsList: fresh ? nextList.data.data : this.data.goodsList.concat(nextList.data.data),
        goodsListLoadStatus: 0,
      });

      this.goodListPagination.index = pageIndex;
      this.goodListPagination.num = pageSize;
    } catch (err) {
      this.setData({ goodsListLoadStatus: 3 });
    }
  },

  goodListClickHandle(e) {
    // const { index } = e.detail;
    const { id } = e.detail.goods;
    // wx.navigateTo({
    //   url: `/service/shoppingMall/Details/details/index?spuId=${id}`,
    // });
    util.authSkip('/service/shoppingMall/Details/details/index?spuId=' + id)
  },

  goodListAddCartHandle() {
    Toast({
      context: this,
      selector: '#t-toast',
      message: '点击加入购物车',
    });
  },

  handleSubmit(e) {
    const { value } = e.detail
    wx.navigateTo({
      url: '/service/shoppingMall/Goods/list/index?searchValue=' + value,
    });
  },

  navToSearchPage() {
    wx.navigateTo({ url: '/service/shoppingMall/Goods/search/index' });
  },

  navToActivityDetail({ detail }) {
    const { index: promotionID = 0 } = detail || {};
    wx.navigateTo({
      url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
    });
  },

  cartView(e) {
    const { url } = e.currentTarget.dataset;
    util.authSkip(url)
  }
});
